package com.systop.ltbook.book.subject.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.systop.core.service.BaseGenericsManager;
import com.systop.ltbook.book.subject.model.Subject;

/**
 * 学科管理类
 * @author DU
 */
@SuppressWarnings("unchecked")
@Service
public class SubjectManager extends BaseGenericsManager<Subject> {

	/**
	 * 取得所有的学科
	 */
  public List<Map> getAllSubjects() {
		List list = this.getDao().query("from Subject su");
		if (CollectionUtils.isNotEmpty(list)){
      return toMapArray(list);  
    }
		return list;
	}
	
	/**
	 * 将list中的对象转换为Map对象
	 * @param list
	 * @return list of map
	 */
  private static List<Map> toMapArray(List<Subject> list) {
		List<Map> result = new ArrayList(list.size());
		for (Subject subject : list) {
			Map item = new HashMap();
			item.put("sjId", subject.getId());
			item.put("sjName", subject.getName());
			result.add(item);
		}
		return result;
	}
  
  public Subject getSubjectById(Integer subjectId) {
  	return getDao().get(Subject.class, subjectId);
  }
}
